﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormInvoke
{
    public partial class Form1 : Form
    {
        Action DInitGridDataSource1;
        Action DInitGridDataSource2;
        Action DInitGridDataSource3;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }


        void DataSource1()
        {
            this.BeginInvoke(new Action(() => {
                this.label1.Text += "\nDataSource1 begin";
            }));
            System.Threading.Thread.Sleep(3000);
            List<Human> lHumen = new List<Human>();
            for (int i = 0; i < 30; i++)
            {
                Human human = new Human()
                {
                    ID="1"+i.ToString(),
                    NAME="张"+i.ToString(),
                    AGE=i
                };
                lHumen.Add(human);
            }

            this.BeginInvoke(new Action(() =>
            {
                System.Threading.Thread.Sleep(3000);
                this.gridControl1.DataSource = lHumen;
                this.BeginInvoke(new Action(() => {
                    this.label1.Text += "\ngridControl1 end";
                }));
            }));

            System.Threading.Thread.Sleep(5000);
            this.BeginInvoke(new Action(()=> {
                this.label1.Text += "\nDataSource1 end";
            }));
        }
        void DataSource2()
        {
            this.BeginInvoke(new Action(() => {
                this.label1.Text += "\nDataSource2 begin";
            }));
            System.Threading.Thread.Sleep(3000);
            List<Human> lHumen = new List<Human>();
            for (int i = 0; i < 30; i++)
            {
                Human human = new Human()
                {
                    ID="2"+i.ToString(),
                    NAME="李"+i.ToString(),
                    AGE=i
                };
                lHumen.Add(human);
            }

            this.BeginInvoke(new Action(() =>
            {
                System.Threading.Thread.Sleep(3000);
                this.gridControl2.DataSource = lHumen;
                this.BeginInvoke(new Action(() => {
                    this.label1.Text += "\ngridControl2 end";
                }));
            }));

            System.Threading.Thread.Sleep(5000);
            this.BeginInvoke(new Action(()=> {
                this.label1.Text += "\nDataSource2 end";
            }));
        }
        void DataSource3()
        {
            this.BeginInvoke(new Action(() => {
                this.label1.Text += "\nDataSource3 begin";
            }));

            System.Threading.Thread.Sleep(3000);
            List<Human> lHumen = new List<Human>();
            for (int i = 0; i < 30; i++)
            {
                Human human = new Human()
                {
                    ID="1"+i.ToString(),
                    NAME="王"+i.ToString(),
                    AGE=i
                };
                lHumen.Add(human);
            }

            this.BeginInvoke(new Action(() =>
            {
                System.Threading.Thread.Sleep(3000);
                this.gridControl3.DataSource = lHumen;
                this.BeginInvoke(new Action(() => {
                    this.label1.Text += "\ngridControl3 end";
                }));
            }));

            System.Threading.Thread.Sleep(5000);
            this.BeginInvoke(new Action(()=> {
                this.label1.Text += "\nDataSource3 end";
            }));
        }

        class Human
        {
            public string ID { get; set; } = string.Empty;
            public string NAME { get; set; } = string.Empty;
            public int AGE { get; set; } = 0;
        }

        private void Form1_Shown(object sender, EventArgs e)
        {

            this.label1.Text += $"\n{DateTime.Now} Form1_Shown";
            System.Threading.Thread.Sleep(3000);

            LoadData();
        }


        void LoadData()
        {
            DInitGridDataSource1 += DataSource1;
            DInitGridDataSource2 += DataSource2;
            DInitGridDataSource3 += DataSource3;

            this.label1.Text += $"\n{DateTime.Now} DInitGridDataSource1.BeginInvoke";
            DInitGridDataSource1.BeginInvoke(null, null);

            this.label1.Text += $"\n{DateTime.Now} DInitGridDataSource2.BeginInvoke";
            DInitGridDataSource2.BeginInvoke(null, null);


            this.label1.Text += $"\n{DateTime.Now} DInitGridDataSource3.BeginInvoke";
            DInitGridDataSource3.BeginInvoke(null, null);

            this.label1.Text += $"\n{DateTime.Now} Form1_Showned";
        }
    }
}
